package com.kgc.activityprovider.listener;

import com.kgc.activityprovider.service.WorkFlowUtilService;
import com.kgc.activityprovider.util.GetBeanUtil;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.Date;

//执行监听器 (Execution Listener)(开始和结束节点)

public class MyExecutionListener implements ExecutionListener {


    private Logger logger = LoggerFactory.getLogger(getClass());

    private Expression status;
    private Expression className;
    @Override
    public void notify(DelegateExecution execution) {
        String eventName1 = execution.getEventName();
        if(eventName1.equals("start")){
            return;
        }
        //参数
        String code = (String)execution.getVariable("code");
        Integer checkManId = (Integer) execution.getVariable("checkManId");
        String checkManName = (String) execution.getVariable("checkManName");
        String checkComment = (String) execution.getVariable("checkComment");
        String classNameStr = className.toString();
        int statusInt = Integer.parseInt(status.toString());


        //获取当前流程的key
        String key = execution.getProcessInstanceBusinessKey();
        WorkFlowUtilService workFlowUtilService = (WorkFlowUtilService) GetBeanUtil.getBean(classNameStr);
        //更改状态
        workFlowUtilService.changeStatus(key,statusInt);
        workFlowUtilService.insertCheck(code,checkManId,checkComment);



        String eventName = execution.getEventName();

        // 根据事件类型处理逻辑
        if ("start".equals(eventName)) {
            logger.debug("流程实例ID: " + execution.getProcessInstanceId() + " 已启动");
        } else if ("end".equals(eventName)) {
            logger.debug("流程实例ID: " + execution.getProcessInstanceId() + " 已结束");
        }
    }

}
